package cn.kgc.springboot.controller;

import cn.kgc.springboot.entity.MessageBoard;
import cn.kgc.springboot.service.MessageService;
import cn.kgc.springboot.util.RedisUtil;
import cn.kgc.springboot.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author mosin
 * date 2021/12/16
 * @version 1.0
 */
@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService;
    //提交留言
    @RequestMapping("/commit")
    public Object addMessage(MessageBoard messageBoard, HttpServletRequest request){
        //获取用户的ip
        String remoteAddr = request.getRemoteAddr().equals("0:0:0:0:0:0:0:1") ? "127.0.0.1":request.getRemoteAddr();
        messageBoard.setDeviceId(remoteAddr);
        //设置系统时间
        messageBoard.setCreateTime(new Date());
        //将留言持久化
       Result result =  messageService.addMessage(messageBoard);
       return result;
    }


    @RequestMapping("/temp")
    public Object addTemp(MessageBoard messageBoard, HttpServletRequest request){
        //获取用户的ip
        String remoteAddr = request.getRemoteAddr().equals("0:0:0:0:0:0:0:1") ? "127.0.0.1":request.getRemoteAddr();
        messageBoard.setDeviceId(remoteAddr);
        //设置系统时间
        messageBoard.setCreateTime(new Date());
        //将留言暂存
        Result result =  messageService.addTempMessage(messageBoard);
        return result;
    }

    //查询用户暂存信息
    @RequestMapping("/findtemp")
    public Object findTemp(HttpServletRequest request){
        //获取用户的ip
        String remoteAddr = request.getRemoteAddr().equals("0:0:0:0:0:0:0:1") ? "127.0.0.1":request.getRemoteAddr();
        //查询暂存
        Result result =  messageService.findTemp(remoteAddr);
        return result;
    }


    @RequestMapping("/findall")
    public Object findAll(){
        //查询所有
        Result result =  messageService.findAll();
        return result;
    }

}
